home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 November / EnigmA AMIGA RUN 02 (1995)(G.R. Edizioni)(IT)[!][issue 1995-11][Skylink CD].iso / earcd / program / misc / bgui12.lha / demos / BGUIPlayer / Timer.c < prev   
C/C++ Source or Header  |  1995-05-16  |  4KB  |  144 lines

  1. /*
  2.  *      TIMER.C
  3.  */
  4.  
  5. #include "BGUIPlayer.h"
  6.  
  7. /*
  8.  *      Export symbols.
  9.  */
  10. Prototype BOOL SetupTimer( void );
  11. Prototype VOID KillTimer( void );
  12. Prototype VOID TriggerTimer( ULONG micros );
  13. Prototype BOOL CheckTimer( void );
  14. Prototype ULONG TimerMask;
  15.  
  16. struct MsgPort          *TimerPort = NULL;
  17. ULONG                    TimerMask = 0L;
  18. struct IORequest        *TimerReq = NULL;
  19.  
  20. /*
  21.  *      Some macros.
  22.  */
  23. #define SET_COMMAND(r,c)          (( struct timerequest * )r )->tr_node.io_Command = c
  24. #define SET_SECONDS(r,s)          (( struct timerequest * )r )->tr_time.tv_secs    = s
  25. #define SET_MICROS(r,m)           (( struct timerequest * )r )->tr_time.tv_micro   = m
  26. #define GET_ERROR(r)              (( struct timerequest * )r )->tr_node.io_Error
  27.  
  28. /*
  29.  *      Open up the timer.device.
  30.  */
  31. BOOL SetupTimer( void )
  32. {
  33.         /*
  34.          *      Create a port.
  35.          */
  36.         if ( TimerPort = CreateMsgPort()) {
  37.                 /*
  38.                  *      Create timer request block.
  39.                  */
  40.                 if ( TimerReq = CreateIORequest( TimerPort, sizeof( struct timerequest ))) {
  41.                         /*
  42.                          *      Open the device.
  43.                          */
  44.                         if ( ! OpenDevice( TIMERNAME, UNIT_VBLANK, TimerReq, 0L )) {
  45.                                 /*
  46.                                  *      Set global port mask.
  47.                                  */
  48.                                 TimerMask = ( 1L << TimerPort->mp_SigBit );
  49.                                 /*
  50.                                  *      Initialize it.
  51.                                  */
  52.                                 SET_COMMAND( TimerReq, TR_ADDREQUEST );
  53.                                 return( TRUE );
  54.                         }
  55.                         DeleteIORequest( TimerReq );
  56.                         TimerReq = NULL;
  57.                 }
  58.                 DeleteMsgPort( TimerPort );
  59.                 TimerPort = NULL;
  60.         }
  61.         return( FALSE );
  62. }
  63.  
  64. /*
  65.  *      Close up the timer.device.
  66.  */
  67. VOID KillTimer( void )
  68. {
  69.         /*
  70.          *      All OK?
  71.          */
  72.         if ( TimerReq ) {
  73.                 /*
  74.                  *      Request pending?
  75.                  */
  76.                 if ( ! CheckIO( TimerReq )) {
  77.                         /*
  78.                          *      Abort.
  79.                          */
  80.                         AbortIO( TimerReq );
  81.                         /*
  82.                          *      Pop aborted request.
  83.                          */
  84.                         WaitIO( TimerReq );
  85.                 }
  86.                 /*
  87.                  *      Close device.
  88.                  */
  89.                 CloseDevice( TimerReq );
  90.                 /*
  91.                  *      Delete request.
  92.                  */
  93.                 DeleteIORequest( TimerReq );
  94.                 /*
  95.                  *      Delete port.
  96.                  */
  97.                 DeleteMsgPort( TimerPort );
  98.         }
  99. }
  100.  
  101. /*
  102.  *      Trigger a timer request.
  103.  */
  104. VOID TriggerTimer( ULONG micros )
  105. {
  106.         /*
  107.          *      All OK?
  108.          */
  109.         if ( TimerReq ) {
  110.                 /*
  111.                  *      Request pending?
  112.                  */
  113.                 if ( ! CheckIO( TimerReq )) {
  114.                         /*
  115.                          *      Abort.
  116.                          */
  117.                         AbortIO( TimerReq );
  118.                         /*
  119.                          *      Pop aborted request.
  120.                          */
  121.                         WaitIO( TimerReq );
  122.                 }
  123.                 /*
  124.                  *      Setup the time-delay.
  125.                  */
  126.                 SET_MICROS( TimerReq, micros );
  127.                 SET_SECONDS( TimerReq, 0L );
  128.                 /*
  129.                  *      Send the request.
  130.                  */
  131.                 SendIO( TimerReq );
  132.         }
  133. }
  134.  
  135. /*
  136.  *      Check if it's a valid timer message.
  137.  */
  138. BOOL CheckTimer( void )
  139. {
  140.         BOOL                    rc = TRUE;
  141.  
  142.         return( GET_ERROR( TimerReq ) ? FALSE : TRUE );
  143. }
  144.